package com.iknight.mgr.core.utils;

import java.math.BigDecimal;

public class IkgNumberUtils {
    public static final double EPSINON = 0.00001;

    /**
     * 对象转Double,带精度
     *
     * @param object
     * @param scale
     * @return
     */
    public static double objectToDoubleInScale(Object object, int scale) {
        double amount = objectToDouble(object);
        BigDecimal bd = new BigDecimal(amount).setScale(scale);//小数点保留两位
        return bd.doubleValue();
    }

    public static double objectToDouble(Object o) {
        if (null == o || o.toString().trim().length() == 0) {
            return 0;
        }
        return Double.parseDouble(o.toString().trim());
    }

    /**
     * Object转int
     *
     * @param o
     * @return
     */
    public static int objectToInt(Object o) {
//        logger.error(o);
        if (null == o) {
            return 0;
        }
        if (o.toString().length() == 0) {
            return 0;
        }
        return Integer.parseInt(o.toString().trim());
    }

    /**
     * double转BigdDecimal
     *
     * @param raw   原始值
     * @param scale 精度
     * @return
     */
    public static BigDecimal roundDouble(double raw, int scale) {
        return new BigDecimal(raw).setScale(4, BigDecimal.ROUND_HALF_UP);
    }
}
